package service.impl;

import dao.CardMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.CardDao;
import service.CardService;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class CardServiceImpl implements CardService {
    private Logger logger = Logger.getLogger(String.valueOf(CardService.class));
    //查询
    @Override
    public List<CardDao> getCardList() {
        SqlSession sqlSession = null;
        List<CardDao> cardDaoList = new ArrayList<>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            cardDaoList = sqlSession.getMapper(CardMapper.class).getCardList();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return cardDaoList;
    }
    //添加
    @Override
    public int addCard(CardDao cardDao) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).addCard(cardDao);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int getCardByIdNumber(String idNumber) {
        SqlSession sqlSession = null;
        int count  =0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(CardMapper.class).getCardByIdNumber(idNumber);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
}
